﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView.EmpBasicManager
{
    public partial class EmpFamilyRelationWindow : ChildWindow
    {
        private EmpbasicService.EmpBasicServiceClient Client = new EmpbasicService.EmpBasicServiceClient();
        private EmpbasicService.Emp_FamilyRelationModel model = new EmpbasicService.Emp_FamilyRelationModel();
        private EmpbasicService.Emp_FamilyRelationModel oldmodel = new EmpbasicService.Emp_FamilyRelationModel();
        private InsertSystemRemark insertsys = new InsertSystemRemark();
        public int ID;
        public int EmpID;
        public bool IsNew = false;
        public EmpFamilyRelationWindow(int empID,int iD, bool isnew)
        {
            InitializeComponent();
            ID = iD;
            EmpID = empID;
            IsNew = isnew;
            if (IsNew)
            {
                this.Title = "新增家庭关系信息";
                model = new EmpbasicService.Emp_FamilyRelationModel();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑家庭关系信息";
                Client.GetEmp_FamilyRelationModelCompleted += new EventHandler<EmpbasicService.GetEmp_FamilyRelationModelCompletedEventArgs>(Client_GetEmp_FamilyRelationModelCompleted);
                Client.GetEmp_FamilyRelationModelAsync("(" + EmpID + ")", ID);
            }
        }

        void Client_GetEmp_FamilyRelationModelCompleted(object sender, EmpbasicService.GetEmp_FamilyRelationModelCompletedEventArgs e)
        {
            if (e.Result != null)
            {
                model = e.Result[0];
                oldmodel = new EmpbasicService.Emp_FamilyRelationModel
                {
                    Name = model.Name,
                    Appellation = model.Appellation,
                    Unit = model.Unit,
                    Duty = model.Duty,
                    Political = model.Political,
                    Birthday = model.Birthday
                };
            }
            this.DataContext = model;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (IsNew == true)
            {
                model.EmpBasicID = EmpID;
                Client.AddEmp_FamilyRelationModelCompleted += new EventHandler<EmpbasicService.AddEmp_FamilyRelationModelCompletedEventArgs>(Client_AddEmp_FamilyRelationModelCompleted);
                Client.AddEmp_FamilyRelationModelAsync(model);
            }
            else
            {
                if (!string.IsNullOrEmpty(begin.Text)) model.Birthday = Convert.ToDateTime(begin.Text);
                else
                    model.Birthday = null;
                Client.UpdateEmp_FamilyRelationModelCompleted += new EventHandler<EmpbasicService.UpdateEmp_FamilyRelationModelCompletedEventArgs>(Client_UpdateEmp_FamilyRelationModelCompleted);
                Client.UpdateEmp_FamilyRelationModelAsync(model);
            }

        }

        void Client_UpdateEmp_FamilyRelationModelCompleted(object sender, EmpbasicService.UpdateEmp_FamilyRelationModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
                InsertSystem("编辑");
                this.DialogResult = true;
            }
        }

        void Client_AddEmp_FamilyRelationModelCompleted(object sender, EmpbasicService.AddEmp_FamilyRelationModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
                InsertSystem("新增");
                this.DialogResult = true;
            }
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
        void InsertSystem(string FangShi)
        {
            if (FangShi == "新增")
            {
                insertsys.Insert("家庭关系子集", "", "", model.EmpBasicID, FangShi, "");
            }
            else
            {
                if (oldmodel.Birthday != model.Birthday)
                    insertsys.Insert("家庭关系子集", oldmodel.Birthday == null ? null : Convert.ToDateTime(oldmodel.Birthday).ToShortDateString(),
                        model.Birthday == null ? null : Convert.ToDateTime(model.Birthday).ToShortDateString(),
                        model.EmpBasicID, FangShi, "出生日期");
                if (oldmodel.Name != model.Name)
                    insertsys.Insert("家庭关系子集", oldmodel.Name, model.Name,
                        model.EmpBasicID, FangShi, "姓名");
                if (oldmodel.Appellation != model.Appellation)
                    insertsys.Insert("家庭关系子集", oldmodel.Appellation, model.Appellation,
                        model.EmpBasicID, FangShi, "称谓");

                if (oldmodel.Unit != model.Unit)
                    insertsys.Insert("家庭关系子集", oldmodel.Unit, model.Unit,
                        model.EmpBasicID, FangShi, "所在单位");
                if (oldmodel.Duty != model.Duty)
                    insertsys.Insert("家庭关系子集", oldmodel.Duty, model.Duty,
                        model.EmpBasicID, FangShi, "职务");
                if (oldmodel.Political != model.Political)
                    insertsys.Insert("家庭关系子集", oldmodel.Political, model.Political,
                        model.EmpBasicID, FangShi, "政治面貌");
            }

        }
    }
}
